from rest_framework import viewsets, mixins
from rest_framework.response import Response

from goods.models import Foods
from goods.serializers import FoodSerializer
from shops.models import ShopsModel, BannerModel, ChildCategoryModel
from shops.serializers import ShopsSerializer, BannerViewSerializer, ChildCategorySerializer
from utils.lat_lng import shops_range


# 商家列表
class ShopsView(viewsets.GenericViewSet,
                mixins.ListModelMixin):

    queryset = ShopsModel.objects.all()

    serializer_class = ShopsSerializer

    def list(self, request, *args, **kwargs):
        child_category_id = request.GET.get('child_category_id')
        sort_id = request.GET.get('sort_id')
        user_latitude = request.GET.get('latitude')
        user_longitude = request.GET.get('longitude')
        print(len(self.get_queryset()))
        max_lat, min_lat, max_lng, min_lng = shops_range(float(user_latitude), float(user_longitude))
        queryset = self.queryset.filter(latitude__lt=max_lat).filter(latitude__gt=min_lat).filter(longitude__lt=max_lng).filter(longitude__gt=min_lng)
        print(len(queryset))
        if child_category_id:
            queryset = queryset.filter(child_category=child_category_id)
            queryset = self.shops_sorted(queryset, sort_id, user_latitude, user_longitude)
        else:
            queryset = self.shops_sorted(queryset, sort_id, user_latitude, user_longitude)
        print(len(queryset))
        serializer = self.get_serializer(queryset, many=True)

        return Response(serializer.data)

    def shops_sorted(self, shops_list, sort_id, user_latitude, user_longitude):
        if sort_id == '1':
            # return sorted(shops_list, key=lambda x: x['min_price'], reverse=False)
            return shops_list.order_by('min_price')
        elif sort_id == '2':
            return shops_list.order_by('-min_price')
        elif sort_id == '3':
            return shops_list.order_by('-score')
        elif sort_id == '4':
            return shops_list.order_by('-month_sales')
        else:
            return shops_list


# 轮播图
class ShopsBannerView(viewsets.GenericViewSet,
                      mixins.ListModelMixin):

    queryset = BannerModel.objects.all()

    serializer_class = BannerViewSerializer

    def get_queryset(self):
        return self.queryset.filter(banner_id__lt=4)

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)

        return Response(serializer.data)


# 二级分类列表
class ChildCategoryView(viewsets.GenericViewSet,
                        mixins.ListModelMixin):

    queryset = ChildCategoryModel.objects.all()

    serializer_class = ChildCategorySerializer

    def list(self, request, *args, **kwargs):
        category_id = request.GET.get('category_id')
        queryset = self.queryset.filter(shop_category=category_id)
        serializer = self.get_serializer(queryset, many=True)

        return Response(serializer.data)


# 商家详情
class ShopsInfoView(viewsets.GenericViewSet,
                    mixins.ListModelMixin):
    queryset = ShopsModel.objects.all()

    serializer_class = ShopsSerializer

    def list(self, request, *args, **kwargs):
        shop_id = request.GET.get('shop_id')
        queryset = self.queryset.filter(shop_id=shop_id)
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


# 商家推荐
class ShopsRecommendView(viewsets.GenericViewSet,
                         mixins.ListModelMixin):
    queryset = Foods.objects.all()

    serializer_class = FoodSerializer

    def list(self, request, *args, **kwargs):
        shop_id = request.GET.get('shop_id')
        foods = self.queryset.filter(shop=shop_id)
        foods = foods.order_by('-month_sales')[0:3]
        serializer = self.get_serializer(foods, many=True)
        return Response(serializer.data)


# 商家搜索
class SearchShopsView(viewsets.GenericViewSet,
                      mixins.ListModelMixin):
    queryset = ShopsModel.objects.all()

    serializer_class = ShopsSerializer

    def list(self, request, *args, **kwargs):
        shop_name = request.GET.get('keywords')
        user_latitude = request.GET.get('latitude')
        user_longitude = request.GET.get('longitude')
        max_lat, min_lat, max_lng, min_lng = shops_range(float(user_latitude), float(user_longitude))
        queryset = self.queryset.filter(latitude__lt=max_lat).filter(latitude__gt=min_lat).filter(longitude__lt=max_lng).filter(longitude__gt=min_lng)
        shops = queryset.filter(shop_name__contains=shop_name)
        serializer = self.get_serializer(shops, many=True)
        return Response(serializer.data)
